import { useCallback } from "react"

type options = {
  wait?: number
}
export const useDebounceFn = (
  fn: (...args: any[]) => void,
  options?: options
) => {
  const debounce = () => {
    let timer: NodeJS.Timeout
    return (...args: any[]) => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        fn.apply(this, args)
      }, options?.wait || 500)
    }
  }
  const run = useCallback( debounce
  ,[debounce,fn])
 
  return { run }
}
